import {h, ref, unref} from 'vue';
import {NSelect, NSwitch, NTag} from 'naive-ui';
import TableItemUrl from "./tableItemUrl.vue"
import {FormSchema} from "@/components/Form";

export interface TableItemAction {
  type: string
  action: Function
}

const tableItemAction = ref<TableItemAction[]>([]);

export const columns = [
  {
    type: 'selection',
    key: 'selection'
  },
  {
    title: "游戏ID",
    key: "gameId",
  },
  {
    title: '游戏名称',
    key: 'gameName',
  },
  {
    title: '游戏描述',
    key: 'gameDesc',
    render(row) {
      return (row.gameDesc || row.gameDesc == '') ? "无" : row.gameDesc
    }
  }, {
    title: '状态',
    key: 'status',
    render(row) {
      return h(TableItemUrl, {
        itemValue: row.status,
        onUpdate: actionFun("UpdateStatus"),
        itemKey: row
      })
    }
  }, {
    title: '分成比例',
    key: 'proportion',
  }, {
    title: 'CP支付回调',
    key: 'payCallbackUrl',
    render(row) {
      return h(TableItemUrl, {
        isEdit: true,
        itemValue: row.payCallbackUrl,
        itemType: "CP支付回调",
        onUpdate: actionFun("CpPayCallback"),
        itemKey: row
      })
    }
  }, {
    title: '游戏链接',
    key: 'gameUrl',
    render(row) {
      return h(TableItemUrl, {
        isEdit: true,
        itemValue: row.gameUrl,
        itemType: "游戏链接",
        onUpdate: actionFun("GameUrl"),
        itemKey: row
      })
    }
  }, {
    title: '实名认证',
    key: 'cardAuth',
    render(row) {
      return h(
        NTag,
        {
          type: row.cardAuth == 'able' ? 'success' : 'error',
        },
        {
          default: () => (row.cardAuth == 'able' ? '需要认证' : '不需要认证'),
        }
      );
    },
  }
];


function actionFun(type: string): Function {
  for (let action of unref(tableItemAction)) {
    if (type === action.type) {
      return action.action;
    }
  }
  return () => {
  }
}

export function registerHandler(actions: TableItemAction[]) {
  tableItemAction.value = actions
}
